package com.cctc.framework.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cctc.framework.domain.EntityForGrid;
import com.cctc.framework.domain.LoggerInfo;
import com.cctc.framework.util.XmlUtil;

@Controller
@RequestMapping("/system")
public class LoggerController
{
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
    
    @ResponseBody
    @RequestMapping(value="/log.json")
    public EntityForGrid getAllLogger(HttpServletRequest request)
    {
        String location = LoggerController.class.getResource("/").getPath() + "log4j.xml";
        Document doc = null;
        try
        {
            doc = XmlUtil.getDocument(location);
        }
        catch (DocumentException e)
        {
            e.printStackTrace();
        }
        Element root = doc.getRootElement();
        Iterator<Element> itr = root.elements("logger").iterator();
        String name = null;
        String level = null;
        Logger logger = null;
        LoggerInfo li = null;
        Element category = null;
        List<LoggerInfo> list = new ArrayList<LoggerInfo>();
        while (itr.hasNext())
        {
            category = itr.next();
            name = category.attributeValue("name");
            logger = Logger.getLogger(name);
            if (logger != null && logger.getLevel() != null)
            {
                level = logger.getLevel().toString();
                li = new LoggerInfo(name, level);
            }
            else
            {
                li = new LoggerInfo(name, category.element("level").attributeValue("value").toUpperCase());
            }
            list.add(li);
        }
        request.setAttribute("list", list);
        EntityForGrid eg=new EntityForGrid(list.size(), list);
        System.out.println(eg);
        return eg;
    }
    
    @RequestMapping(value = "/update.html", method = RequestMethod.POST)
    public String updateLoggerLevel(@RequestParam String[] logger, @RequestParam String[] level)
    {
        for (int i = 0; i < logger.length; i++)
        {
            Logger log = Logger.getLogger(logger[i]);
            log.setLevel(Level.toLevel(level[i]));
        }
        return "redirect:/system/log";
    }
    
//    @ResponseBody
//    @RequestMapping(value = "/1.html")
//    public String u(@RequestParam("d") Date d)
//    {
//        System.out.println(d.getTime());
//        //System.out.println(body);
//        return "123457890";
//    }
}

